package com.neusoft.demo.controller;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.demo.po.Goods;
import com.neusoft.demo.po.Order;
import com.neusoft.demo.service.OrderService;

@RestController
public class OrderController {
	@Autowired
	private OrderService service;
	
	    /**
	     * 根据用户id获取该用户订单，无开始结束时间参数则为查询全部订单
	     * 
	     * @param userId
	     * @return
	     */
		@RequestMapping(value= {"/get_order"},method= {RequestMethod.POST,RequestMethod.GET})
		public ModelAndView findOrderByUserId(@RequestParam(value="Uid",required=false)Integer userId,
				 @RequestParam(value="begin_time",required=false)Date beginTime,
				 @RequestParam(value="end_time",required=false)Date endTime,ModelAndView mv) {
			try {
			  Order order = new Order();
			  order.setUid(userId);
			  order.setBeginTime(beginTime);
			  order.setEndTime(endTime);
			  List<Order> list = service.findByUserId(order);
			  System.out.println(list);
			  mv.addObject("list", list);
			  mv.setViewName("dingdanzhongxin");
			  return mv;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;	
		}
		
		@RequestMapping(value= {"/get_order_by_orderCode"},method= {RequestMethod.POST,RequestMethod.GET})
		public ModelAndView findOrderByOrderCode(@RequestParam(value="order_code",required=false)BigInteger orderCode,
				 @RequestParam(value="reality_price",required=false)float realityPrice,ModelAndView mv) {
			try {
			  Order order = new Order();
			  order.setOrderCode(orderCode);
			  List<Goods> list = service.findByOrderCode(order);
			  System.out.println(list);
			  mv.addObject("list", list);
			  mv.setViewName("dingdanxianqing");
			  return mv;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;	
		}
		
		@RequestMapping(value= {"/alter_order_status"},method= {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public boolean alterOrderStatus(@RequestParam(value="orderId",required=true)Integer orderId,
										@RequestParam(value="status",required=true)Integer status) {
			Order order =new Order();
			try {
				order.setStatus(status);
				order.setOrderId(orderId);
				boolean b = service.alterStatus(order);
				return b;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
		
		@RequestMapping(value= {"/delete_order"},method= {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public boolean alterOrderStatus(@RequestParam(value="orderId",required=true)Integer orderId) {
			Order order =new Order();
			try {
				order.setVisible(false);
				order.setOrderId(orderId);
				boolean b = service.deleteOrders(order);
				return b;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
}
